package com.work.dao;

import com.work.main.Main;
import com.work.mapper.AreaStatMapper;
import com.work.mapper.StatisticsMapper;
import com.work.mapper.TimeLineMapper;
import com.work.ncov.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * @ClassName InformationDao
 * @Description: dao层
 * @Author: WangWenpeng
 * @date: 19:35 2020/1/31
 * @Version 1.0
 */
public class InformationDao {
    private SqlSession session;
    private InputStream is;
    private static final Logger logger = Logger.getLogger(Main.class);

    public InformationDao() {
        try {
            is = Resources.getResourceAsStream("mybatis.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //使用工厂设计模式
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //生产SqlSession
        session = factory.openSession();
    }

    /**
     * @Description 关连接
     * @Author WangWenpeng
     * @Date 19:49 2020/1/31
     * @Param []
     */
    public void destory() {
        try {
            is.close();
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description 记录消息timeLine
     * @Author WangWenpeng
     * @Date 20:12 2020/1/31
     * @Param [timeLine]
     */
    public int recordTimeLineInfo(TimeLineServiceInfo timeLine) {
        TimeLineMapper timeLineMapper = session.getMapper(TimeLineMapper.class);
        int res = timeLineMapper.recordTimeLineInfo(timeLine);
        logger.info("记录消息开始");
        if (res == 1) {
            logger.info("记录消息成功，" + timeLine.getId());
        }
        logger.info("记录消息结束--结果： " + res);
        session.commit();
        return res;
    }

    //  statistics全国信息统计数据begin

    /**
     * @Description 查询历史表的数据
     * @Author WangWenpeng
     * @Date 15:24 2020/2/1
     * @Param [statisticsInfo]
     */
    public int queryStatisticCountByModTime(StatisticsInfo statisticsInfo) {
        StatisticsMapper statisticsMapper = session.getMapper(StatisticsMapper.class);
        int count = statisticsMapper.queryStatisticCountByModTime(statisticsInfo);
        return count;
    }

    /**
     * @Description 记录全国数据统计记录历史数据统计表
     * @Author WangWenpeng
     * @Date 13:27 2020/2/1
     * @Param [statisticsInfo]
     */
    public int recordStatisticHisInfo(StatisticsInfo statisticsInfo) {

        StatisticsMapper statisticsMapper = session.getMapper(StatisticsMapper.class);
        int res = statisticsMapper.recordStatisticHisInfo(statisticsInfo);
        logger.info("记录全国历史数据统计开始");
        if (res == 1) {
            logger.info("记录全国历史数据统计成功，" + statisticsInfo.getId());
        }
        logger.info("记录全国历史数据统计结束--结果： " + res);
        session.commit();
        return res;
    }

    /**
     * @Description 删除全国信息统计表数据
     * @Author WangWenpeng
     * @Date 15:41 2020/2/1
     * @Param []
     */
    public int deleteStatisticInfo() {
        StatisticsMapper statisticsMapper = session.getMapper(StatisticsMapper.class);
        int res = statisticsMapper.deleteStatisticInfo();
        return res;
    }

    /**
     * @Description 记录全国信息统计表
     * @Author WangWenpeng
     * @Date 15:40 2020/2/1
     * @Param [statisticsInfo]
     */
    public int recordStatisticInfo(StatisticsInfo statisticsInfo) {
        StatisticsMapper statisticsMapper = session.getMapper(StatisticsMapper.class);
        int res = statisticsMapper.recordStatisticInfo(statisticsInfo);
        logger.info("记录全国数据统计开始");
        if (res == 1) {
            logger.info("记录全国数据统计成功，" + statisticsInfo.getId());
        }
        logger.info("记录全国数据统计结束--结果： " + res);
        session.commit();
        return res;
    }
    //  statistics全国信息统计数据end

    // areaStat省市数据begin

    /**
     * @Description 删除省级数据信息
     * @Author WangWenpeng
     * @Date 17:17 2020/2/1
     * @Param []
     */
    public int deleteProvinceInfo() {
        AreaStatMapper areaStatMapper = session.getMapper(AreaStatMapper.class);
        int rst = areaStatMapper.deleteProvinceInfo();
        session.commit();
        return rst;
    }

    /**
     * @Description 批量新增省份数据信息的信息
     * @Author WangWenpeng
     * @Date 17:18 2020/2/1
     * @Param [areaStatProvinceInfoList]
     */
    public int recordProvinceInfo(List<AreaStatProvinceInfo> areaStatProvinceInfoList) {
        AreaStatMapper areaStatMapper = session.getMapper(AreaStatMapper.class);
        int rst = areaStatMapper.recordProvinceInfo(areaStatProvinceInfoList);
        session.commit();
        return rst;
    }

    /**
     * @Description 删除市级数据信息
     * @Author WangWenpeng
     * @Date 17:20 2020/2/1
     * @Param []
     */
    public int deleteCityInfo() {
        AreaStatMapper areaStatMapper = session.getMapper(AreaStatMapper.class);
        int rst = areaStatMapper.deleteCityInfo();
        session.commit();
        return rst;
    }

    /**
     * @Description 批量新增市级信息
     * @Author WangWenpeng
     * @Date 17:20 2020/2/1
     * @Param [areaStatCityInfoList]
     */
    public int recordCityInfo(List<AreaStatCityInfo> areaStatCityInfoList) {
        AreaStatMapper areaStatMapper = session.getMapper(AreaStatMapper.class);
        int rst = areaStatMapper.recordCityInfo(areaStatCityInfoList);
        session.commit();
        return rst;
    }
    // areaStat省市数据end

}
